<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\model;
use think\Model;
use think\facade\Db;

class Ad extends Model{
    /**列表
    * signid 1显示9已删除，大于9就是删除时间戳
    * lang 语言
    * att_type 广告类型：banner幻灯片，link友情链接
    * keyword 关键词
    * order 排序
    */
	static function index($signid,$lang,$att_type,$keyword,$order){
		$keyword = trim($keyword); $where[] = ['lang','=',$lang];

		$pageParam = array('lang'=>$lang);

		if(!empty($att_type)){
			$where[] = ['att_type','=',$att_type];
			$pageParam += array('att_type'=>$att_type);
		}

		if($signid > 8){//已软删除
			$where[] = ['signid','>','8'];
		}else{
			$where[] = ['signid','=',$signid];
		}
		$pageParam += array('signid'=>$signid);

		$pageParam += array('keyword'=>$keyword);

		$rs = Db::name('ad')->where($where)->where('title|description','like',"%$keyword%")->order($order)
			->paginate([
            	'list_rows'=> 20,
            	'query'=> $pageParam,
            ]);
		return $rs;
	}
    
    //位置
    protected function setPositionAttr(){
	    $position=implode(',',input('position/a'));
		return $position;
    }

    //客户端
    protected function setClientAttr(){
        $client = '';
		$client_array=input('client/a');
        for($i=0;$i<count($client_array);$i++){
			$i<count($client_array)-1 ? $client.=$client_array[$i]."," : $client.=$client_array[$i];
	    }
		return $client;
    }
	
    //新增编辑
	static function edit($id){
	    if($id > 0){//编辑
			$ad = Ad::find($id);
		}else{//新增
			$ad = new Ad;
		}
		$ad->save(input('post.'));
		return $ad->id;
	}
}